﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.SMS.Common;

namespace Wicresoft.SMS.Data
{
    public class sqlEmailDataAccess : IEmailDataAccess
    {
        private IConnectStringProvider _connParamProvider;

        public sqlEmailDataAccess(IConnectStringProvider configProvider)
        {
            _connParamProvider = configProvider;

        }

        public List<Email> GetUnsendMails()
        {
            try
            {
                using (var dbacc = new Entities(_connParamProvider.ConnectionString))
                {
                    var mails = dbacc.Email.Where(e => e.State == false).ToList<Email>();
                    return mails;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("sqlEmailDataAccess.GetUnsendMails()" + ex.Message);
                return null;
            }
        }

        public void MarkEmailStateAsSent(IEnumerable<Email> emails)
        {
            if (emails == null)
            {
                Logger.Error("sqlEmailDataAccess.MarkEmailStateAsSent()" + " emails is NULL");
                return;
            }
            try
            {
                using (var dbacc = new Entities(_connParamProvider.ConnectionString))
                {
                    foreach (var item in emails)
                    {
                        var em = dbacc.Email.FirstOrDefault(e => e.ID == item.ID);
                        if (em != null)
                        {
                            em.State = item.State;
                            em.ModifyTime = item.ModifyTime;
                        }
                        else
                        {
                            Logger.Error(string.Format("sqlEmailDataAccess.MarkEmailStateAsSent() Cannot find the record {0}", item.ID));
                        }
                    }
                    dbacc.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                Logger.Error("sqlEmailDataAccess.MarkEmailStateAsSent()" + ex.Message);
            }
        }


        public bool SendEmail(List<Email> emails)
        {
            if (emails == null)
            {
                Logger.Error("sqlEmailDataAccess.SendEmail() " + " email is NULL");
                return false;
            }
            try
            {
                Logger.Debug("Entry SendEmail");
                using (var dbacc = new Entities(_connParamProvider.ConnectionString))
                {
                    foreach (var email in emails)
                    {
                        //Email em = new Email();
                        //em.To = "";
                        //if (email.UserID != 0)
                        //{
                        //    var userinfo = dbacc.User.FirstOrDefault(u => u.UserID == email.UserID);
                        //    if (userinfo != null)
                        //    {
                        //        em.To = userinfo.Email + ";";
                        //    }
                        //    else
                        //    {
                        //        Logger.Error(string.Format("sqlEmailDataAccess.SendEmail() Cannot find user {0}", email.UserID));
                        //    }
                        //    em.UserID = email.UserID;
                        //}
                        email.CreateTime = DateTime.Now;
                        email.State = false;
                        dbacc.AddToEmail(email);  
                    }
                    
                    dbacc.SaveChanges();
                    Logger.Debug("Leaving SendEmail");
                    return true;
                }
            }
            catch (Exception ex)
            {
                Logger.Error("sqlEmailDataAccess.SendEmail() " + ex.Message);
                return false;
            }

        }
    }
}
